/*
 * Copyright (C) Michael Opitz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA
 */
#ifndef XML_FILETYPE_H
#define XML_FILETYPE_H

// using libxml2 for xml-parsing...
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

#include "parser.h"
#include "parse_exception.h"
#include "network.h"
#include "filetype.h"

#include "../model/parser.h"
#include "../model/dom_policy.h"
#include "../model/dom.h"

#include <boost/shared_ptr.hpp>

#include <string>
#include <boost/regex.hpp>

using namespace std;

/** 
 * \brief This namespaces includes all nertwork related stuff.
 * \author Michael Opitz
 */
namespace network {
	/** 
	 * \brief This class implements a xml_filetype, which can convert
	 * dom-nodes to formatted strings, and parse formatted strings to
	 * xml-nodes.
	 */
	class xml_filetype : public network::filetype {
	public:
		xml_filetype();
		virtual ~xml_filetype();

		/** 
		 * \brief parse parses a formatted string.
		 * 
		 * \param msg is the formatted string which should be parsed
		 * 
		 * \return a shared_ptr to a dom-node is returned.
		 */
		virtual boost::shared_ptr<model::dom> parse(
			const string &msg);

		/** 
		 * \brief send converts a dom-node to a formatted string
		 * 
		 * \param dom is the dom-node which should be converted
		 * 
		 * \return a formatted string is returned.
		 */
		virtual string send(boost::shared_ptr<model::dom> dom);
	private:
		boost::shared_ptr<model::dom> convert(xmlDocPtr ptr);
		void recur_conv(xmlNode *ptr, boost::shared_ptr<model::dom>);
		void conv_attrib(xmlAttr *attr, 
			boost::shared_ptr<model::dom>);
	
		string _buff;
		string _start_tag;
		bool _end_in_start;
		static int _num_filetypes;
		static boost::mutex _xml_mutex;
	};
}

#endif
